#!/bin/bash
# Time: 2023-10-20 12:44:57
# Desc: 初始化git仓库

usage() {
    echo "Usage:"
    echo "    $0"
}

init_suite() {
    if [ $# -ne 0 ]; then
        echo "the script need no args"
        usage "$@"
        return 1
    fi

    local target
    target="$(dirname "$(dirname "$(realpath "$0")")")"
    if [ "$(basename "$target")" != "common" ]; then
        echo "the submodule dir name must be 'common'"
        return 1
    fi
    target="$(dirname "$target")"
    local tmpdir
    tmpdir="$(mktemp -d)"

    echo "try git clone https://gitee.com/opencloudos-testing/tsuite-demo.git"
    if ! git clone https://gitee.com/opencloudos-testing/tsuite-demo.git "$tmpdir"; then
        echo "clone the tsuite-demo fail"
        rm -rf "$tmpdir"
        return 1
    fi

    rm -r "$tmpdir"/testcase/test_*
    for i in cmd kmod Makefile Makefile.common testcase lib tsuite; do
        cp -r "$tmpdir/$i" "$target"
    done
    cat "$tmpdir"/.gitignore >>"$target/.gitignore"
    rm -rf "$tmpdir"

    echo "add files: git add * .gitignore"
    echo "   commit: git commit -asm 'init tsuite project'"

    return 0
}

init_suite "$@"
